
#include "member_BRender.h"
#include "member_BLight.h"

using namespace BWE;

member_BRender::member_BRender(BRender* render)
{
	boss = render;
	camera = 0;
	node = 0;
	shape = 0;
	texture = 0;
	material = 0;
	lights = 0;

	lighting = true;
	cullback = true;
	depthTest = true;
	normalize = false;
	pointSmooth = false;
	pointSize = 1;
	lineSmooth = false;
	lineWidth = 1;
	polygonMode = Polygon_Fill;
	polygonOffset = 0;
}
member_BRender::~member_BRender()
{

}

void member_BRender::useLights(const BVector& point)
{
	if (lights == 0)
		return;

	BLight*	light_array[8];
	for (int i = 0; i < 8; i++)
	{
		light_array[i] = (*lights)(i);
	}
	for (int i = 0; i < 8; i++)
	{
		BLight* light = light_array[i];
		int index = GL_LIGHT0 + i;
		if (!light)
		{
			if (i > 0)
				glDisable(index);
			continue;
		}
		glEnable(index);

		glLightfv(index, GL_AMBIENT, (const GLfloat*)&light_member(light)->ambientColor);
		glLightfv(index, GL_DIFFUSE, (const GLfloat*)&light_member(light)->diffuseColor);
		glLightfv(index, GL_SPECULAR, (const GLfloat*)&light_member(light)->specularColor);
		glLightf(index, GL_CONSTANT_ATTENUATION, 1.0f);
		glLightf(index, GL_LINEAR_ATTENUATION, light->attenuation());
		glLightf(index, GL_QUADRATIC_ATTENUATION, light->attenuation2());

		BVector position = light->position();
		if (light_member(light)->type == BLight::Type_Point)
		{
			GLfloat pos[4] = { position.x(), position.y(), position.z(), 1.0f };
			glLightfv(index, GL_POSITION, pos);
			continue;
		}
		if (light_member(light)->type == BLight::Type_Spot)
		{
			BVector direction = light->direction();
			GLfloat pos[4] = { position.x(), position.y(), position.z(), 1.0f };
			GLfloat dir[3] = { direction.x(), direction.y(), direction.z() };
			glLightfv(index, GL_POSITION, pos);
			glLightfv(index, GL_SPOT_DIRECTION, dir);
			glLightf(index, GL_SPOT_CUTOFF, light->cutoff());
			glLightf(index, GL_SPOT_EXPONENT, light->exponent());
			continue;
		}
		if (light_member(light)->type == BLight::Type_Direct)
		{
			GLfloat pos[4] = { position.x(), position.y(), position.z(), 0.0f };
			glLightfv(index, GL_POSITION, pos);
			continue;
		}
	}
}
